<?php
namespace src;
use classlib\acl;
use classlib\core\controller;
use classlib\core as C;
use classlib\core\lang;
use src\logic\Cadmin;
class Clogin extends controller {
	/**
	 * 会员登录页面资料信息
	 * Enter description here ...
	 */
	public function main() {
		$acl = acl::inst();
		if ($acl->islogin()) {
			$acl->signout();
		}
		$lang = C\lang::inst('adm');
		$view = C\view::inst('html');
		$slang= $lang->get('login');
		$view->assign_by_ref('lang', $slang);
		$view->display('login.html');
	}
	
	/**
	 * 验证登录的接口
	 * Enter description here ...
	 */
	public function check() {
		$view   = C\view::inst('json');
		$lang   = C\lang::inst('adm');
		$filter = new \classlib\validator\auditor();
		$filter->lang($lang);
		try {
			$account = $filter->reset('account')->required()->maxlen(127)->nospace()->run();
			$loginpw = $filter->reset('loginpw')->required()->maxlen(127)->nospace()->run();
			$vcode   = $filter->reset('vcode')->required()->alphanum()->run();
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		if (!\classlib\captcha\simple::verify($vcode)) {
			$view->assign_error(10019, $lang->txt('login', 10019));
		}
		$user = C\model::load('main.sys.user')->get_item(array('account'=>$account));
		if (empty($user)) {
			$view->assign_error(10020, $lang->txt('login', 10020));
		}
		$acl = acl::inst();
		if (!$acl->check($loginpw, $user['loginpw'])) {
			$view->assign_error(10021, $lang->txt('login', 10021));
		}
		if ($user['status'] == 0) {
			$view->assign_error(10022, $lang->txt('login', 10022));
		}
		if ($user['expire'] && $user['expire'] < C\config::$nowtime) {
			$view->assign_error(10023, $lang->txt('login', 10023));
		}
		//是否允许多终端登录 1-不允许 后提前
		if ($user['isdup'] == 1) {
			$user['loginpw'] .= C\config::$nowtime;
		}
		$loginpw = $acl->ss($user['id'], $user['loginpw']);
		$lusr = array('id'=>$user['id'], 'account'=>$user['account'], 'nick'=>$user['nickname']);
		if($acl->signin($lusr, $loginpw)) {//记录登录日志
			$acl->user = $lusr;
			Cadmin::log2db('[login]'.$_SERVER['HTTP_USER_AGENT'].'['.C\get_ip().']');
			$view->assign('msg', C\router::get_url('admin'));
			$view->display();
		} else {
			$view->assign_error(10024, $lang->txt('login', 10024));
		}
	}
}